package cm.com.carlamanda.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import cm.com.carlamanda.domain.InputParam;
import cm.com.carlamanda.domain.Page;
import cm.com.carlamanda.service.InputParamService;
import cm.com.carlamanda.util.StringUtil;
@Controller
@RequestMapping(value="inputParam")
public class InputParamController {
    @Autowired
    private InputParamService inputParamService ;
    
    @RequestMapping(value="add",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject addInputParam(@RequestBody InputParam inputParam){
        JSONObject jsonObject = new JSONObject();
        inputParamService.add(inputParam);
        jsonObject.put("result",true);
        return jsonObject;
    }
    @RequestMapping(value="get",method=RequestMethod.GET)
    @ResponseBody
    public JSONObject getInputParam(@RequestParam(value="id") String id){
        JSONObject jsonObject = new JSONObject();
        InputParam mo =inputParamService.get(id);
        jsonObject.put("data", mo);
        jsonObject.put("result",true);
        return jsonObject;
    }
    
    @RequestMapping(value="search",method=RequestMethod.GET)
    @ResponseBody
    public JSONObject searchInputParam(@RequestParam("page") int page,@RequestParam("pageSize") int pageSize,
            String name ,String description,String projectName,String sortColumn,String sortAsc){
        InputParam inputParam =new  InputParam();
        inputParam.setDescription(description);
        inputParam.setName(name);
        Page pa=new Page();
        pa.setPage(page);
        pa.setPageSize(pageSize);
        if(StringUtil.isNotEmpty(sortColumn)){
            pa.setSortColumn(sortColumn);
            pa.setSortAsc("desc".equalsIgnoreCase(sortAsc)?"desc":"asc");
        }else{
            pa.setSortColumn("create_time");
            pa.setSortColumn("asc");
        }
        List<InputParam> list =inputParamService.search(inputParam,pa);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", list);
        jsonObject.put("result",true);
        jsonObject.put("page", pa);
        return jsonObject;
    }
    
    @RequestMapping(value="del",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject delInputParam(@RequestParam(value="id") String id){
        InputParam inputParam = new InputParam();
        inputParam.setId(id);
        inputParamService.del(inputParam);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",true);
        return jsonObject;
    }
    
    @RequestMapping(value="update",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject updateInputParam(@RequestBody InputParam InputParam){
        inputParamService.update(InputParam);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",true);
        return jsonObject;
    }
}
